package cn.pzaily.fealetter.service.impl;

import cn.pzaily.crab.tools.core.utils.ObjectUtils;
import cn.pzaily.fealetter.entity.SignatureEntity;
import cn.pzaily.fealetter.enumeration.RedisPropertiesKey;
import cn.pzaily.fealetter.mapper.SignatureMapper;
import cn.pzaily.fealetter.service.SignatureService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @PackageName: cn.pzaily.fealetter.service.impl
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-21 20:24
 */
@Service
public class SignatureServiceImpl extends
        ServiceImpl<SignatureMapper, SignatureEntity> implements SignatureService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SignatureEntity findSignatureByCode(String code) {
        ValueOperations<String, SignatureEntity> ops = redisTemplate.opsForValue();
        SignatureEntity signatureEntity = ops.get(RedisPropertiesKey.SIGNATURE_CODE + code);
        if (ObjectUtils.isNull(signatureEntity)) {
            signatureEntity = baseMapper.selectOne(new LambdaQueryWrapper<SignatureEntity>().eq(SignatureEntity::getCode, code));
            if (ObjectUtils.isNotNull(signatureEntity)) {
                ops.set(RedisPropertiesKey.SIGNATURE_CODE + code, signatureEntity, RedisPropertiesKey.TIME, RedisPropertiesKey.TIME_UNIT);
            }
        }
        return signatureEntity;
    }
}
